package com.yidu.boss.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.boss.sys.domain.Exchange;
import com.yidu.boss.sys.service.impl.ExchangeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:cj
 * @Date:2019/12/03
 * @Description:com.yidu.boss.sys.controller
 * @Version:1.0
 */
@Controller
@RequestMapping("/exchange/")
public class ExchangeController {
    @Autowired
    private ExchangeServiceImpl exchangeService;
    /**
     * 分页方法算法
     *
     * @param exchangePageInfo 分页后的对象
     * @param size             分页的条数
     * @return 返回一个用于前端数据的map数据格式
     */
    private Map<String, Object> pageALG(PageInfo exchangePageInfo, long size) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", size);
        map.put("data", exchangePageInfo.getList());
        return map;
    }
    /**
     * 跳转交易品种费率页面
     * @return
     */
    @RequestMapping("exchangeJsp")
    public String exchangeJsp(){
        return "exchange";
    }
    /**
     * 查询所有品种费率
     * @return
     */
    @RequestMapping("exchangeList")
    @ResponseBody
    public Object doSelectAllExchange(Integer page,Integer limit){
        //System.out.println("列表");
        //页面大小，页面
        PageHelper.startPage(page,limit);
        //根据条件查询管理人数据
        List<Exchange> exchangeList=exchangeService.selectByExample(null);
        //使用pageInfo包装查询后的结果，封装了详细的查询数据，其中参数是列表数据
        PageInfo<Exchange> exchangePageInfo =new PageInfo<Exchange>(exchangeList);
        //列表数据
        List<Exchange> exchangeData =exchangePageInfo.getList();
        //map集合
        Map<String,Object> map =pageALG(exchangePageInfo,exchangePageInfo.getTotal());
        return map;
    }
    /**
     * 添加
     * @param exchange
     * @return
     */
    @RequestMapping("insertExchange")
    @ResponseBody
    public String addExchange(Exchange exchange){
        int i =exchangeService.insertExchange(exchange);
        System.err.println("添加");
        if(i > 0){
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 修改
     * @param exchange
     * @return
     */
    @RequestMapping("updateExchange")
    @ResponseBody
    public String updateExchange(Exchange exchange){
        Exchange example =new Exchange();
        example.setExchangeId(exchange.getExchangeId());
        example.setExchangeName(exchange.getExchangeName());
        example.setStampDuty(exchange.getStampDuty());
        example.setTransferRate(exchange.getTransferRate());
        example.setHandlingFeeRate(exchange.getHandlingFeeRate());
        example.setCollectionRate(exchange.getCollectionRate());
        example.setDescription(exchange.getDescription());
        int i =exchangeService.updateExchange(example);
        if(i > 0){
            return "修改成功";
        }
        return "修改失败";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping("deleteExchange")
    @ResponseBody
    public String deleteExchange(String[] ids){
        System.err.println("删除");
        int i = 0;
        for(String id:ids){
            i += exchangeService.deleteExchange(id);
        }
        if(i > 0){
            return "删除成功";
        }
        return "删除失败";
    }
    /**
     * 模糊查询档案
     *
     * @param condition 模糊查询条件
     * @param page      页码
     * @param limit     数据条数
     * @return 返回前端数据格式
     */
    @RequestMapping("selectExchangeLike")
    @ResponseBody
    public Object selectLike(String condition,Integer page, Integer limit) {
        //System.out.println(exchange);
        //判断模糊查询条件参数是否为空
        if(condition.equals("")){
            //为空显示所有列表页面
            Object o =doSelectAllExchange(page,limit);
            return o;
        }
        //开始页
        PageHelper.startPage(page,limit);
        //调用业务层方法
        List<Exchange> exchangeList =exchangeService.selectSomeByMore(condition);
        PageInfo<Exchange>exchangePageInfo =new PageInfo<Exchange>(exchangeList);
        //调用分页算法
        Map<String, Object> map =pageALG(exchangePageInfo,exchangePageInfo.getTotal());
        return map;
    }
}
